package com.core.shared;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Json转换器.
 * <ul>
 * <li>依赖的jar包有：jackson-core-1.9.5.jar、jackson-mapper-1.9.5.jar</li>
 * </ul>
 */
public abstract class JsonUtil {

    private static ObjectMapper mapper = new ObjectMapper();    //只创建一次->性能


    //***************************基本*****************************

    /**
     * 将对象转换为JSON字符串.
     */
    public static String toJson(Object object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将对象转换为JSON流，通过response输出.
     */
    public static void toJson(HttpServletResponse response, String contentType, Object value) {
        try {
            response.setContentType(contentType);
            mapper.writeValue(response.getWriter(), value);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将对象转换为JSON流，通过response输出.
     */
    public static void toJson(HttpServletResponse response, Object value) {
        try {
            mapper.writeValue(response.getWriter(), value);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将JSON字符串转换为对象，通过valueType指定对象类型.
     */
    public static <T> T toObject(String json, Class<T> valueType) {
        try {
            if (json == null) {
                return null;
            }
            return mapper.readValue(json, valueType);
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 将JSON字符串转换为对象，通过typeReference指定对象类型.
     */
    public static <T> T toObject(String json, TypeReference<?> typeReference) {
        try {
            if (json == null) {
                return null;
            }
            return mapper.readValue(json, typeReference);
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 将JSON字符串转换为对象，通过javaType指定对象类型.
     */
    public static <T> T toObject(String json, JavaType javaType) {
        try {
            if (json == null) {
                return null;
            }
            return mapper.readValue(json, javaType);
        } catch (IOException e) {
            return null;
        }
    }


    //***************************互转*****************************

    /**
     * map转换为指定类型对象（经过json中转，先转为字符串，再转为指定类型对象）.
     */
    public static <T> T mapToObject(Map<?, ?> map, Class<T> valueType) {
        String json = toJson(map);
        return toObject(json, valueType);
    }

    /**
     * 对象转换为map（经过json中转，先转为字符串，再转为map）.
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> objectToMap(Object obj) {
        String json = toJson(obj);
        return toObject(json, Map.class);
    }

}